using System;
using GameFramework.Event;
using UnityEngine;
using Unity.VisualScripting;

namespace LS.UGFUnits
{
    [UnitShortTitle("Open")]
    [UnitSurtitle("UI Form")]
    public sealed class OpenUIForm : UIUnit
    {
        [DoNotSerialize]
        [PortLabelHidden]
        public ValueInput AssetName { get; private set; }
        
        [DoNotSerialize]
        [PortLabelHidden]
        public ValueInput GroupName { get; private set; }
        
        [DoNotSerialize]
        [PortLabelHidden]
        public ValueOutput SerialId { get; private set; }
        
        protected override void Definition()
        {
            base.Definition();
            AssetName = ValueInput<string>(nameof(AssetName), string.Empty);
            GroupName = ValueInput<string>(nameof(GroupName), string.Empty);
            SerialId = ValueOutput<int>(nameof(SerialId));
            Requirement(AssetName, Input);
            Requirement(GroupName, Input);
            Succession(Input, Output);
        }

        protected override ControlOutput In(Flow flow)
        {
            string assetName = flow.GetValue<string>(AssetName);
            string groupName = flow.GetValue<string>(GroupName);
            int serialId = Target?.OpenUIForm(assetName, groupName) ?? -1;
            flow.SetValue(SerialId, serialId);
            return base.In(flow);
        }
    }
}